<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Models\MsgGroup;
use Illuminate\Support\Facades\Cache;
use App\Modules\Admin\Http\Requests\MessageCreateRequest;
use App\Modules\Admin\Http\Requests\MessageUpdateRequest;
use App\Modules\Admin\Http\Controllers\BaseController;
use App\Services\Admin\MessageService;
use App\Models\MsgInfo;
use Illuminate\Http\Request;


class MessageController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $param = $this->getParam('Message','Message/getList');
        return view('admin::message.index',compact('param'));
    }

    /**
     * 获取列表页
     * @param Request $request
     * @method getList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getList(Request $request){
        Cache::forever('Message/getList', $request->server->get('QUERY_STRING'));
        $page = $request->input('page', 1);
        $per_page = $request->input('limit', 10);
        $messageService = new MessageService();
        $result = $messageService->getList($page,$per_page);
        if($result['status']==1){
            return layTableApi($result['data']['total'],$result['data']['data']);
        }else{
            return layTableApi(0,[],1);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $types = MsgInfo::getTypes();
        return view('admin::message.create',compact('types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     *
     * @param RoleCreateRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(MessageCreateRequest $request)
    {
        $data = $request->all();
        if(empty($data['detail'])&&empty($data['url'])){
            return apiResponse('请填写消息详情或链接');
        }
        Cache::forever('Message', true);
        $messageService = new MessageService();
        $result = $messageService->createMsg($data);
        if($result['status']==1){
           return apiResponse($result['msg'],$result['status'],$result['data']);
        }else{
           return apiResponse($result['msg']);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $msg = MsgInfo::findOrFail($id);
        $group = MsgGroup::from('msg_group as a')->leftJoin('user_info as b','a.user_id','=','b.id')->where('a.msg_id',$id)->select('b.id','b.nickname')->get()->toArray();
        $group = json_encode($group);
        $types = MsgInfo::getTypes();
        return view('admin::message.edit', compact('msg','types','group'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function detail($id)
    {
        $msg = MsgInfo::findOrFail($id);
        $group = MsgGroup::from('msg_group as a')->leftJoin('user_info as b','a.user_id','=','b.id')->where('a.msg_id',$id)->select('b.id','b.nickname')->get()->toArray();
        $group = json_encode($group);
        $types = MsgInfo::getTypes();
        return view('admin::message.detail', compact('msg','types','group'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param RoleUpdateRequest $request
     * @param  int $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(MessageUpdateRequest $request)
    {
        $data = $request->all();
        Cache::forever('Message', true);
        if(empty($data['detail'])&&empty($data['url'])){
            return apiResponse('请填写消息详情或链接');
        }
        $messageService = new MessageService();
        $result = $messageService->updateMsg($data);
        if($result['status']==1){
           return apiResponse($result['msg'],$result['status'],$result['data']);
        }else{
           return apiResponse($result['msg']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request)
    {
        //
        $ids = $request->get('ids');
        if (empty($ids)) {
            return apiResponse('请选择删除项');
        }
        if (MsgInfo::where('id',$ids)->update(['status'=>3])) {
            return apiResponse('删除成功！',1,[]);
        }
        return apiResponse('删除失败！');
    }


}
